package chuangyi.com.org.DOMIHome.Fragments;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Resources;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import chuangyi.com.org.ViewModel.ContextObservable;

/**
 * Created by zxh on 16/11/17.
 * 通用Frgment类，打开此Fragment时必须为其传递layoutId参数
 */
public class CommonFragment extends Fragment {

    public interface Callback {
        void viewCreated(View root);
    }

    private ContextObservable dataModel;
    private int variableId;
    private
    @LayoutRes
    int layoutId;
    private Callback initCallback;
    private ViewDataBinding binding;

    public Callback getInitCallback() {
        return initCallback;
    }

    public void setInitCallback(Callback initCallback) {
        this.initCallback = initCallback;
    }

    public ContextObservable getDataModel() {
        return dataModel;
    }

    public void setDataModel(ContextObservable dataModel) {
        this.dataModel = dataModel;
        if (binding != null) {
            binding.setVariable(variableId, this.dataModel);
        }
    }

    public int getVariableId() {
        return variableId;
    }

    public void setVariableId(int variableId) {
        this.variableId = variableId;
    }

    public int getLayoutId() {
        return layoutId;
    }

    public void setLayoutId(@LayoutRes int layoutId) {
        this.layoutId = layoutId;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, layoutId, container, false);
        initView(binding.getRoot());
        if (dataModel != null) {
            binding.setVariable(variableId, dataModel);
        }
        return binding.getRoot();
    }

    protected void initView(View view) {
        if (initCallback != null) {
            initCallback.viewCreated(view);
        }
    }
    
    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }


    @Override
    public void onDetach() {
        super.onDetach();
    }
}
